package browserUI
{
	import browserUI.assets.backBtn;
	import browserUI.assets.favouritesBtn;
	import browserUI.assets.forwardBtn;
	import browserUI.assets.homeBtn;
	import browserUI.assets.settingsBtn;
	
	import dev.touchswipe.shared.Fonts;
	
	import flash.display.GradientType;
	import flash.display.InteractiveObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.ui.Keyboard;
	
	public class Navbar extends Sprite
	{
		private var textFrame:Shape;
		private var _width:Number=500;
		public var urlInput:TextField;
		
		private var _backBtn:backBtn;
		private var _forwardBtn:forwardBtn;
		
		private var _tabs:Vector.<Tab>;
		private var _newTabButton:Sprite;
		private var _currentTab:int;
		private var _settingsButton:settingsBtn;
		private var _settingsPanel:SettingsPanel;
		private var _homeBtn:homeBtn;
		private var _favouritesBtn:favouritesBtn;
		private var _favouritesPanel:FavouritesPanel;
		
		public function Navbar()
		{
			super();
			
			_backBtn=new backBtn();
			_backBtn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{
				dispatchEvent(new BrowserEvent(BrowserEvent.BACK_COMMAND));
			});
			addChild(_backBtn);
			
			_forwardBtn=new forwardBtn();
			_forwardBtn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{
				dispatchEvent(new BrowserEvent(BrowserEvent.FORWARD_COMMAND));
			});
			addChild(_forwardBtn);
			 
			_tabs=new Vector.<Tab>(4);
			_newTabButton=new Sprite();
			_newTabButton.graphics.beginFill(0xE7E6EB);
			_newTabButton.graphics.drawRoundRectComplex(5,0,50,30,15,15,0,0);
			_newTabButton.graphics.beginFill(0xA7A9AC);
			_newTabButton.graphics.drawRect(27.5,7.5,5,15);
			_newTabButton.graphics.beginFill(0xA7A9AC);
			_newTabButton.graphics.drawRect(22.5,12.5,15,5);
			_newTabButton.y=30;
			_newTabButton.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{
				dispatchEvent(new BrowserEvent(BrowserEvent.NEW_TAB_COMMAND));
			});
			addChild(_newTabButton);
			
			textFrame=new Shape();
			textFrame.y=2.5;
			textFrame.x=10;
			addChild(textFrame);
			
			_settingsButton=new settingsBtn();
			_settingsButton.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{
				_settingsPanel.open=!_settingsPanel.open;
				if(_settingsPanel.open)
					_favouritesPanel.open=false;
			});
			addChild(_settingsButton);
			
			_settingsPanel=new SettingsPanel();
			addChild(_settingsPanel);
			
			
			
			_homeBtn=new homeBtn();
			_homeBtn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{
				var evt:BrowserEvent=new BrowserEvent(BrowserEvent.BROWSE_COMMAND);
				evt.url=MTBrowser.currentSettings.homeURL;
				dispatchEvent(evt);
			});
			addChild(_homeBtn);
			
			_favouritesBtn=new favouritesBtn();
			addChild(_favouritesBtn);
			_favouritesBtn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{
				_favouritesPanel.open=!_favouritesPanel.open;
				if(_favouritesPanel.open)
					_settingsPanel.open=false;
			});
			
			_favouritesPanel=new FavouritesPanel();
			_favouritesPanel.addEventListener(UICommandEvent.CREATE_FAVOURITE,function(event:UICommandEvent):void{
				dispatchEvent(event);
			});
			addChild(_favouritesPanel);
			
			urlInput=new TextField();
			urlInput.embedFonts=true;
			urlInput.defaultTextFormat=new TextFormat(Fonts.SegoeUIL,15);
			urlInput.height=30;
			urlInput.x=15;
			urlInput.y=2.5;
			urlInput.type=TextFieldType.INPUT;
			urlInput.addEventListener(KeyboardEvent.KEY_UP,function(event:KeyboardEvent):void{
				if(event.keyCode==Keyboard.ENTER)
					browseToURL();
			});
			addChild(urlInput);
		}
		
		public function addTab(id:int):void{
			var tab:Tab=new Tab(id);
			tab.y=30;
			_tabs[id]=tab;
			tab.addEventListener(UICommandEvent.SELECT_TAB,function (event:UICommandEvent):void{
				dispatchEvent(event);
			});
			tab.addEventListener(UICommandEvent.CLOSE_TAB,function (event:UICommandEvent):void{
				dispatchEvent(event);
			});
			addChild(tab);
		}
		
		public function removeTab(id:int):void{
			removeChild(_tabs[id]);
			_tabs[id]=null
			redraw();
		}
		
		public function selectTab(id:int):void{
			if(_tabs[_currentTab])
				_tabs[_currentTab].active=false;
			_tabs[id].active=true;
			_currentTab=id;
			redraw();
		}
		
		public function setTabTitle(id:int,title:String):void{
			_tabs[id].setTitleText(title);
		}
		
		public function browseToURL():void{
			var url:String=urlInput.text;
			if(url.search(/^(ht|f)tp(s?)\:\/\//)==-1)
				url="http://"+url;
			var evt:BrowserEvent=new BrowserEvent(BrowserEvent.BROWSE_COMMAND);
			evt.url=url;
			dispatchEvent(evt);
		}
		
		public function get url():String{
			return urlInput.text;
		}
		
		public function set url(url:String):void{
			urlInput.text=url;
		}
		
		public override function get width():Number{
			return _width;
		}
		public override function set width(value:Number):void{
			_width=value;
			redraw();
		}
		
		private function redraw():void{
			graphics.clear();
			var m:Matrix=new Matrix();
			m.createGradientBox(60,60,-Math.PI/2,0,0);
			graphics.beginGradientFill(GradientType.LINEAR,[0xA0A2A5,0xE8E8E8],[1,1],[0,255],m);
			graphics.drawRect(0,0,_width,60);
			
			textFrame.graphics.clear();
			textFrame.graphics.beginFill(0xFFFFFF);
			textFrame.graphics.drawRoundRect(0,0,_width-360,25,14);
			
			urlInput.width=_width-375;
			
			var a:int=0;
			for(var t:int=0; t<_tabs.length; t++){
				if(_tabs[t]){
					_tabs[t].x=a*200;
					a++;
				}
			}
			_newTabButton.x=200*a;
			if(a==4)
				_newTabButton.visible=false;
			else
				_newTabButton.visible=true;
			
			_settingsButton.x=_width-75;
			_homeBtn.x=_width-330;
			_backBtn.x=_width-270;
			_forwardBtn.x=_width-210;
			_favouritesBtn.x=_width-150;
			
			_favouritesPanel.x=_width;
			_favouritesPanel.y=60;
			_settingsPanel.x=_width;
			_settingsPanel.y=60;
			
		}
	}
}